package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("department")
public class DepartmentController {
	@Autowired
   private IDepartmentService departmentService;

	@RequestMapping("")
	@PermissionName("部门列表")
	@RequiresPermissions("department:index")
	public String index(){
		return "department/list";
	}

    @RequestMapping("list")
	@ResponseBody
	@PermissionName("部门列表")
	@RequiresPermissions("department:list")
	public PageResult list(QueryObject qo){
	    
        return departmentService.query(qo);
	}

	//保存
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("部门保存")
    @RequiresPermissions("department:save")
    public JsonResult save(Department entity){
        JsonResult result = new JsonResult();
        try{
            departmentService.insert(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.markMsg("保存失败");
        }
        return  result;
    }
	//保存
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("部门编辑")
    @RequiresPermissions("department:update")
    public JsonResult update(Department entity){
        JsonResult result = new JsonResult();
        try{
            departmentService.updateByPrimaryKey(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.markMsg("更新失败");
        }
        return  result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("department:changeState")
    @PermissionName("部门状态")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            departmentService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }

    //查部门
    @RequestMapping("queryDepts")
    @ResponseBody
    public List<Department> queryDepts(){

        return departmentService.selectAll();
    }

}
